16. Joining Tables

Joining Tables

a

In the video, there are two tables, animals and diet. Joining those tables gives us a new table, which contains information taken from the two original tables. The join statement has a join condition that tells the database how to match up the rows from one table with the rows from the other.


Table reference

Here are the tables from the video. They're the same ones as earlier in this lesson.

animals:

name string species string birthdate date
Max gorilla 2001-04-13
Sue gorilla 1998-06-12
Max moose 2012-02-20
Alison llama 1997-11-24
George gorilla 2011-01-09
Spot iguana 2010-07-23
Ratu orangutan 1989-09-15
Eli llama 2002-02-22

diet:

species string food string
llama plants
brown bear fish
brown bear meat
brown bear plants
orangutan plants
orangutan insects

This SQL query will join the two tables to find out what foods each animal can eat:

select animals.name, animals.species, diet.food
       from animals join diet
       on animals.species = diet.species;

You'll see much more with these tables later in the course!